<?php


namespace app\common\validate;


use app\common\enum\MainEnum;

class OrderValidate extends CommonValidate
{
    protected $rule  = [
        'id|ID'                          => 'require|integer|between:1,100000000',
        'address_id|地址ID'                => 'require|integer|between:1,100000000',
        'refund_id|退款申请ID'               => 'require|integer|between:1,100000000',
        'nb|订单编号'                        => 'require|length:10,50',
        'package_company_id|快递公司ID'      => 'require|integer|between:1,100000000',
        'package_nb|快递单号'                => 'require|length:1,50',
        'receiver_name|收货人'              => 'require|length:1,45',
        'receiver_mobile_number|收货人电话号码' => 'require',
        'receiver_address|收货地址'          => 'require|length:1,255',
        'pay_type|支付类型'                  => 'require|enumPayType',
        'cart_ids|购物车IDS'                => 'require|enumIds',
        'list_id|商品规格'                   => 'require|integer',
        'list_ids|商品规格'                  => 'require|enumIds',
        'remark|卖家留言'                    => 'length:1,255',
        'cancel_desc|取消订单的原因'            => 'length:1,255',
        'create_time|下单时间'               => 'require|enumYmdHis',
        'pay_time|付款时间'                  => 'require|enumYmdHis',
        'status|订单状态'                    => 'require|enumOrderStatus',
        'qs_status|退款签收状态'               => 'require|integer',
        'type|状态'                        => 'require|integer',
        'reason|退款原因'                    => 'require|length:1,255',
        'reason_desc|退款原因描述'             => 'length:0,255',
        'reason_desc_img_url|退款原因描述图'    => 'array',
        'tk_price|退款金额'                  => 'require|float',
        'yanhuo_status|验货状态'             => 'require|bool',
    ];
    protected $scene = [
        'putOrderRefundLuru'    => [
            'list_id',
            'package_company_id',
            'package_nb',
        ],
        'putRefundShouHuo'      => [
            'refund_id',
        ],
        'putRefundYanhuo'       => [
            'refund_id',
            'yanhuo_status',
        ],
        'putRefundApprove'      => [
            'refund_id',
        ],
        'putRefundRefuse'       => [
            'refund_id',
        ],
        'getRefundList'         => [
            'id',
        ],
        'getOrderById'          => [
            'id',
        ],
        'getOrderPackage'       => [
            'id',
        ],
        'postOrder'             => [],
        'putOrderFh'            => [
            'id',
            'order_list_ids',
            'package_company_id',
            'package_nb',
            'receiver_name',
            'receiver_mobile_number',
            'receiver_address',
        ],
        'deleteOrder'           => ['id'],
        'putOrderCancel'        => [
            'id',
            'cancel_desc',
        ],
        'putOrderRefund'        => [
            'id',
            'nb',
            'list_id',
            'qs_status',
            'type',
            'reason',
            'reason_desc',
            'reason_desc_img_url',
            'tk_price',
        ],
        'putOrderSignFor'       => [
            'id',
        ],
//        'putOrderRefundConfirm' => [
//            'id',
//            'cancel_desc',
//        ],
        'postOrderPlace'        => [
            'address_id',
            'cart_ids',
            //            'coupon_id',
            'remark',
        ],
    ];

    protected function enumOrder($value): ?bool
    {
        try {
            if (false !== filter_var($value, FILTER_VALIDATE_INT)) {
                $rule = '1,1000000';
                if (is_string($rule)) {
                    $rule = explode(',', $rule);
                }
                [
                    $min,
                    $max,
                ] = $rule;

                return $value >= $min && $value <= $max;
            }
            if ($value === 'null') {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    protected function enumPayType($value): bool
    {
        try {
            if (in_array($value, [
                'miniapp',
                'wechat',
                'ali',
            ], true)) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    protected function enumOrderStatus($value): bool
    {
        try {
            if (in_array((int)$value, [
                MainEnum::ORDER_STATUS_WAIT_PAY,
                MainEnum::ORDER_STATUS_WAIT_SHIPMENT,
                MainEnum::ORDER_STATUS_WAIT_SIGN_FOR,
                MainEnum::ORDER_STATUS_SIGN_FOR_ED,
                MainEnum::ORDER_STATUS_DONE_BUT_WAIT_APPRAISE,
                MainEnum::ORDER_STATUS_DONE_AND_APPRAISE,
                MainEnum::ORDER_STATUS_CANCEL,
                MainEnum::ORDER_STATUS_REFUND,
            ], true)) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    protected function sceneGetOrderList()
    {
        return $this->only([
            'nb',
            'package_nb',
            'receiver_name',
            'receiver_mobile_number',
            'create_time',
            'pay_time',
            'status',
        ])
            ->remove('nb', 'require')
            ->remove('package_nb', 'require')
            ->remove('receiver_name', 'require')
            ->remove('receiver_mobile_number', 'require')
            ->remove('create_time', 'require')
            ->remove('pay_time', 'require')
            ->remove('status', 'require');
    }
}